package cn.net.ecode.tools.ecodeplatformmaker.dialog;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.ui.PlatformUI;

import cn.net.ecode.tools.ecodeplatformmaker.util.LogOut;
import cn.net.ecode.tools.ecodeplatformmaker.util.DBManager;
import cn.net.ecode.tools.ecodeplatformmaker.util.OperateXml;
import cn.net.ecode.tools.ecodeplatformmaker.view.ConnTree;


public class EditConn extends Dialog {
	public static final String ID = "cn.net.ecode.tools.ecodeplatformmaker.dialog.AddConn"; //$NON-NLS-1$
	private Map connection;
	private Text socket;
	private Text ip;
	private Text sid;
	private Text userName;
	private Text userPwd;
	private Button isdefault;
	private Text uuid;
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public EditConn(Shell parentShell) {
		super(parentShell);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		if(this.connection != null)
		{
			newShell.setText("修改数据库连接");
		}
		else
		{
			newShell.setText("新增数据库连接");
		}
		
	}
	
	private void initializeForm()
	{
		if(this.connection != null)
		{
			ip.setText((String)connection.get("ip"));
			socket.setText((String)connection.get("port"));
			sid.setText((String)connection.get("sid"));
			userName.setText((String)connection.get("account"));
			userPwd.setText((String)connection.get("password"));
			isdefault.setSelection(("yes").equals((String)connection.get("default"))?true:false);
			uuid.setText((String)connection.get("uuid"));
		}
	}
	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 4;
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("IP:");
		
		ip = new Text(container, SWT.BORDER);
		GridData gd_ip = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_ip.widthHint = 135;
		ip.setLayoutData(gd_ip);
		
		Label lblNewLabel_1 = new Label(container, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("端口:");
		
		socket = new Text(container, SWT.BORDER);
		socket.setText("1521");
		GridData gd_socket = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_socket.widthHint = 135;
		socket.setLayoutData(gd_socket);
		
		Label lblNewLabel_2 = new Label(container, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText("SID:");
		
		sid = new Text(container, SWT.BORDER);
		sid.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_3 = new Label(container, SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_3.setText("用户名:");
		
		userName = new Text(container, SWT.BORDER);
		userName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_4 = new Label(container, SWT.NONE);
		lblNewLabel_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_4.setText("密码:");
		
		userPwd = new Text(container, SWT.BORDER);
		userPwd.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_5 = new Label(container, SWT.NONE);
		lblNewLabel_5.setText("设为默认:");
		
		isdefault = new Button(container, SWT.CHECK);
		new Label(container, SWT.NONE);
		
		uuid = new Text(container, SWT.BORDER);
		uuid.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		uuid.setVisible(false);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		initializeForm();
		return container;
	}


	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.NEXT_ID) {
			DBManager dm = new DBManager();
			String flag = dm.testConnect(ip.getText(), socket
					.getText(), sid.getText(), userName
					.getText(), userPwd.getText());
			MessageBox mb = new MessageBox(this.getShell(), SWT.OK
					| SWT.ERROR_CANNOT_GET_SELECTION);
			mb.setText("提示");
			if (flag.equals("good")) {
				LogOut.info("测试数据库连接成功");
				mb.setMessage("数据库连接成功！");
			} else {
				mb.setMessage(flag);
			}
			mb.open();
			return ;
		}
		else if (buttonId == IDialogConstants.OK_ID) {
			Map conn = new HashMap();
			conn.put("ip", ip.getText());
			conn.put("port", socket.getText());
			conn.put("sid", sid.getText());
			conn.put("account", userName.getText());
			conn.put("password", userPwd.getText());
			conn.put("default", isdefault.getSelection()?"yes":"no");
			conn.put("uuid", uuid.getText());
			try {
				OperateXml.saveConn(conn);
				ConnTree view = (ConnTree)PlatformUI.getWorkbench().getActiveWorkbenchWindow().   
                getActivePage().findView(ConnTree.ID);
				view.refreshData();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		super.buttonPressed(buttonId);
	}
	
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button testbtns = createButton(parent, IDialogConstants.NEXT_ID, IDialogConstants.OK_LABEL,
				true);
		testbtns.setText("测试连接(&T)");
		Button btns = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				false);
		btns.setText("保存(&S)");
		Button button = createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
		button.setText("取消(&C)");
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 200);
	}

	public void init(Map map)
	{
		this.connection = map;
	}
}
